package org.jeecg.modules.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.course.entity.AstuCourse;
import org.jeecg.modules.experiment.entity.AstuExperiment;
import org.jeecg.modules.tea_stu_course.entity.AstuTeastuCourse;
import org.jeecg.modules.tea_stu_course.service.IAstuTeastuCourseService;
import org.jeecg.modules.course.service.IAstuCourseService;
import org.jeecg.modules.experiment.service.IAstuExperimentService;
import org.jeecg.modules.experiment_student.entity.AstuExperimentUser;
import org.jeecg.modules.experiment_student.service.IAstuExperimentUserService;
import org.jeecg.modules.score.entity.AstuStudentScore;
import org.jeecg.modules.score.service.IAstuStudentScoreService;
import org.jeecg.modules.student.entity.AstuInformation;
import org.jeecg.modules.student.service.IAstuInformationService;
import org.jeecg.modules.student_course.service.IAstuUserCourseService;
import org.jeecg.modules.teacher_course.service.IAstuTeacherCourseService;
import org.jeecg.modules.teacourse.service.IAstuTeacourseService;
import org.jeecg.modules.vo.AstuTeacherCourseDTO;
import org.jeecg.modules.vo.LoginSysuser;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.base.entity.SysUserTit;
import org.jeecg.common.system.util.JwtUtil;
import org.jeecg.common.util.PasswordUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.vo.Usersubmit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @ClassName:LoginController
 * @Description: TODO
 * @author: wang yue niao
 * @date 2020/11/4 15:18
 * @Version 1.0
 */
@Slf4j
@Api(tags="对外提供的登录接口")
@RestController
@RequestMapping("/LoginControllerinterface")
public class LoginControllerinterface {
    @Autowired
    private ISysBaseAPI sysBaseAPI;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private IAstuInformationService iAstuInformationService;
    @Autowired
    private IAstuCourseService iAstuCourseService;
    @Autowired
    private IAstuExperimentService iAstuExperimentService;
    @Autowired
    private IAstuStudentScoreService iAstuStudentScoreService;
    @Autowired
    private IAstuTeastuCourseService iAstuTeastuCourseService;

    @ApiOperation("对外部登录接口")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Result<JSONObject> login(@RequestBody LoginSysuser sysLoginModel) {
        Result<JSONObject> result = new Result<JSONObject>();
        String username = sysLoginModel.getUsername();//学生的是学号  老师的是老师工号
        String password = sysLoginModel.getPassword();
        String courseNum = sysLoginModel.getCourseNumber();
        System.out.println(username);
        System.out.println(password);
        System.out.println(courseNum);

        //判断账号或者密码是否为空
        if((username==null||username.equals(""))||(password==null)||password.equals("")){
            result.error500("用户名或者密码不能为空");
            return result;
        }

        // 判断用户是否为存在
        SysUserTit sysUserTit = sysBaseAPI.getUser(username);
        if (sysUserTit.getId()==null){//系统用户表 id是否存在
            result.error500("用户不存在");
            return result;
        }

        //校验密码是否正确
        String userpassword = PasswordUtil.encrypt(username, password, sysUserTit.getSalt());
        System.out.println(userpassword);
        String syspassword = sysUserTit.getPassword();
        System.out.println(syspassword);
        if (!syspassword.equals(userpassword)) {
            result.error500("用户名或密码错误");
            System.out.println("用户名密码错误");
            return result;
        }

        //根据传入的课程名称查询课程id 判断课程是否存在
        String courseid = iAstuCourseService.selectIdByCouseNum(courseNum);
        if (courseid==null||courseid.equals("")){
            result.error500("该课程不存在!");
            return result;
        }
        //判断学生是否有选该课程
        AstuTeastuCourse astuTeastuCourse = iAstuTeastuCourseService.selectinfoByStuNumAndCourseNum(username, courseNum);
        if (astuTeastuCourse==null||astuTeastuCourse.equals("")) {
            result.error500("该学生没有对应的课程!");
            return result;
        }
        //登录用户账号
        String username1 = sysUserTit.getUsername();
        System.out.println(username1);
        System.out.println("===============================");
        String token = JwtUtil.sign(username, syspassword);
        // 设置token缓存有效时间
        redisUtil.set(CommonConstant.PREFIX_USER_TOKEN + token, token);
        redisUtil.expire(CommonConstant.PREFIX_USER_TOKEN + token, JwtUtil.EXPIRE_TIME*2 / 1000);

        JSONObject obj = new JSONObject();
        obj.put("token", token);

        List<AstuTeastuCourse> list = iAstuTeastuCourseService.selectInfoByNum(username);
        for (AstuTeastuCourse teastuCourse : list) {
            obj.put("astuTeastuCourse1", teastuCourse);
        }

        result.setResult(obj);
        result.success("登录成功");
        return result;
    }


    @ApiOperation("保存实验成绩")
    @RequestMapping(value = "/submitscore", method = RequestMethod.POST)
    public Result<JSONObject> submitscore(@RequestBody Usersubmit usersubmit){
        String stuNumber = usersubmit.getStudentNumber();
        String score = usersubmit.getScore();
        String courseNum = usersubmit.getCourseNumber();
        String experimentNum = usersubmit.getExperimentSerialNumber();

        System.out.println("学生学号："+stuNumber);
        System.out.println("实验分数："+score);
        System.out.println("课程号"+courseNum);
        System.out.println("实验号"+experimentNum);

        Result<JSONObject> result = new Result<>();
        //根据课程号查找对应的课程id
        String courseid = iAstuCourseService.selectIdByCouseNum(courseNum);
        System.out.println("courseid=====>"+courseid);
        //在判断该课程中是否存在该实验，如果存在返回该实验id
        AstuExperiment astuExperiment = iAstuExperimentService.selectInfoByCourseNumAndExperimentNum(courseNum, experimentNum);
        System.out.println("astuExperiment=====>"+astuExperiment);
        if(astuExperiment==null||astuExperiment.equals("")){
            result.error500("课程不存在该实验");
            return result;
        }
         //根据学生学号和实验名称 判断学生在个人分数中是否存在该实验分数
        AstuStudentScore astuStudentScore  = iAstuStudentScoreService.selectInfoByStuNumAndExperimentNum(stuNumber, experimentNum);
        if(astuStudentScore==null||astuStudentScore.equals("")){
            //如果不存在就存入
            String uuid = UUID.randomUUID().toString();
            AstuStudentScore astuStudentScore1 = new AstuStudentScore();
            //个人成绩 保存id
            astuStudentScore1.setId(uuid);
            //个人成绩 保存课程号
            astuStudentScore1.setCourseId(courseNum);
            // 个人成绩 保存实验成绩
            astuStudentScore1.setExperimentScore(score);
            //个人成绩 保存学生学号
            astuStudentScore1.setStuNumber(stuNumber);
            //个人成绩  保存实验号
            astuStudentScore1.setExperimentId(experimentNum);
            //个人成绩  保存实验批次
            astuStudentScore1.setTermTime(new Date());
            iAstuStudentScoreService.save(astuStudentScore1);
            return result.success("保存成绩成功");
        }else{
            //如果分数存在,可以修改
            iAstuStudentScoreService.updateScoreById(stuNumber,experimentNum,score);
            return result.success("修改成绩成功");
        }
    }
}
